package com.nbjtjc.safe.service.edu;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduRequirementSurveyTempletMapper;
import com.nbjtjc.safe.model.edu.EduRequirementSurveyTemplet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EduRequirementSurveyTempletService {
	@Autowired
	private EduRequirementSurveyTempletMapper eduRequirementSurveyTempletMapper;
	
	public Result add(EduRequirementSurveyTemplet eduRequirementSurveyTemplet){
		Result result = new Result();
		if(ObjectUtil.isNull(eduRequirementSurveyTemplet)){
			result.setFalse("提交内容为空，操作失败");
			return result;
		}
		int res=-1;
		//无ID为新增
		if(ObjectUtil.isNull(eduRequirementSurveyTemplet.getId())){
			res = eduRequirementSurveyTempletMapper.insertSelective(eduRequirementSurveyTemplet);
			if(res == 0){
				result.setFalse("新增失败");
				return result;
			}
		}else{//有ID为更新
			res = eduRequirementSurveyTempletMapper.updateByPrimaryKeySelective(eduRequirementSurveyTemplet);
			if(res == 0){
				result.setFalse("更新失败");
				return result;
			}
		}
		
		result.setData(eduRequirementSurveyTemplet);
		return result;
	}
	
	public Result delete(int id){
		Result result = new Result();
		int res = eduRequirementSurveyTempletMapper.deleteByPrimaryKey(id);
		if(res == 0){
			result.setFalse("删除失败");
			return result;
		}
		return result;
	}
	
	public Result find(EduRequirementSurveyTemplet eduRequirementSurveyTemplet){
		Result result = new Result();
		List<EduRequirementSurveyTemplet> eduRequirementSurveyTemplets = new ArrayList();
		if(ObjectUtil.isNull(eduRequirementSurveyTemplet.getCancel())){
			eduRequirementSurveyTemplet.setCancel(0);
		}
		eduRequirementSurveyTemplets = eduRequirementSurveyTempletMapper.select(eduRequirementSurveyTemplet);
		result.setData(eduRequirementSurveyTemplets);
		return result;
	}
}
